﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;

namespace aucn.web.au
{
    public partial class Inquery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.Attributes["submenu"] = "Services";
            this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Prepare form data
            if (Page.IsValid)
            {
                this.SendMail(txtMail.Text, txtParentName.Text, txtParentTel.Text, txtStudentId.Text, txtStudentName.Text, txtStudentTel.Text, txtContent.Text);
            }
        }

        protected void SendMail(string mailAddress, string pName, string pTel, string sId, string sName, string sTel, string content)
        {


            string[] lines = Regex.Split(ConfigurationManager.AppSettings["MailBodyFormat"], @"\r\n");
            StringBuilder mailBody = new StringBuilder();
            foreach (string s in lines)
            {
                string t = s.Trim();
                if (t.Length > 0)
                {
                    mailBody.AppendLine(t);
                }
            }

            string body = mailBody.ToString()
                .Replace("%pname%", pName)
                .Replace("%ptel%", pTel)
                .Replace("%sid%", sId)
                .Replace("%sname%", sName)
                .Replace("%stel%", sTel)
                .Replace("%content%", content)
                .Replace("%mail%", mailAddress);
                

            MailMessage mm = new MailMessage();
            mm.To.Add(ConfigurationManager.AppSettings["MailAddress"]);
            mm.From = new MailAddress(mailAddress, pName);
            mm.Subject = ConfigurationManager.AppSettings["MailSubjectFormat"].Replace("%id%", sId).Replace("%name", sName);
            mm.BodyEncoding = System.Text.Encoding.UTF8;
            mm.ReplyTo = new MailAddress(mailAddress, pName);
            mm.SubjectEncoding = System.Text.Encoding.UTF8;
            mm.Body = body;
            mm.IsBodyHtml = false;
            SmtpClient client = new SmtpClient();
            client.Credentials = new NetworkCredential("au.sh.china@gmail.com", "a12345678a");
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;


            //client.
            try
            {
                client.Send(mm);
                MultiView1.ActiveViewIndex = 1;
            }
            catch {
                MultiView1.ActiveViewIndex = 2;
            }
        }
    }
}
